// pages/map/search.js
const app = getApp();
const common = require('../../utils/common')
var bmap = require('../../utils/bmap-wx.min.js');
var BMap = new bmap.BMapWX({
  ak: 'TSjvDghIS9WelQRLz2WmWq6w2pdyNXOW'
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityList: [],
    areaList: [],
    streetList: [],
    areaName: '',
    cityName: '',
    streetName: '',
    propertyName: '',
    PROPERTY_TYPE_LIST: common.PROPERTY_TYPE_LIST,
    TRADE_TYPE_LIST: common.TRADE_TYPE_LIST,
    RENT_TYPE_LIST: common.RENT_TYPE_LIST,
    rentFlagName: '',
    tradeTypesName: '',
    params: {},
    address: '', //搜索的地址
    addressList: [], //关联的搜索列表
    longitude: '',
    latitude: ''
  },

  //选择列表名字
  selectName(e){
    this.setData({ address: e.currentTarget.dataset.name, addressList: [] })
  },

  //修改地址栏
  changAddress(e){
    let that = this;
    this.setData({ address: e.detail.value });
    // 更新关联的搜索列表
    BMap.suggestion({
      query: e.detail.value,
      region: this.data.cityName,
      city_limit: true,
      success: function(res){
        if(res.message=='ok'){
          that.setData({ addressList: res.result })
        }
      },
      fail: function(fail) {
        console.log(fail);
      }
    })
  },
  clearInput(){
    this.setData({ address: "", addressList: [] })
  },

  //点击确认返回地图
  ensure(){
    let pages = getCurrentPages();
    let prevPage = pages[pages.length-2];
    let params = this.data.params;
    params.address = this.data.address
    prevPage.setData({ params })
    prevPage.handleSearch();
    setTimeout(()=>{
      wx.navigateBack({
        delta: 1  //想要返回的层级
      })
    },500)
  },

  //选择资产类型
  bindPickerChangeProperty(e){
    let { value } = e.detail;
    let propertyName = this.data.PROPERTY_TYPE_LIST[value].label
    let params = this.data.params;
    params.propertyTypes = this.data.PROPERTY_TYPE_LIST[value].value
    this.setData({ propertyName, params })
  },

  // 修改交易类型
  bindPickerChangeTrade(e){
    let { value } = e.detail;
    let tradeTypesName = this.data.TRADE_TYPE_LIST[value].label
    let params = this.data.params;
    params.tradeTypes = this.data.TRADE_TYPE_LIST[value].value
    this.setData({ tradeTypesName, params })
  },

  //修改租金类型
  bindPickerChangeRent(e){
    let { value } = e.detail;
    let rentFlagName = this.data.RENT_TYPE_LIST[value].label
    let params = this.data.params;
    params.rentFlag = this.data.RENT_TYPE_LIST[value].value
    this.setData({ rentFlagName, params })
  },

  //修改城市
  bindPickerChange(e){
    let { value } = e.detail;
    let cityName = this.data.cityList[value].name
    let params = this.data.params;
    params.cityId = this.data.cityList[value].id
    params.areaId = '';
    params.streetId = '';
    this.setData({ params, cityName, areaName: '', streetName: ''})
    this.getAreaList();
  },
   //修改区县
  bindPickerChangeArea(e){
    let { value } = e.detail;
    let Area = this.data.areaList[value]
    let areaName = Area.name
    let streetList = Area.streets
    let params = this.data.params;
    params.areaId = Area.id
    params.streetId =''
    this.setData({ areaName, params, streetList, streetName: '' })
  },
   //修改街道
  bindPickerChangeStreet(e){
    let { value } = e.detail;
    let Street = this.data.streetList[value]
    let streetName = Street.name
    let params = this.data.params;
    params.streetId = Street.id
    this.setData({ streetName, params })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    let { propertyTypes, tradeTypes, rentFlag, address } = JSON.parse(options.params)
    let propertyName = this.data.PROPERTY_TYPE_LIST.filter(item=>item.value==propertyTypes)[0].label
    let trade = this.data.TRADE_TYPE_LIST.filter(item=>item.value==tradeTypes);
    let tradeTypesName = trade.length>0?trade[0].label:""
    let rentFlagName = this.data.RENT_TYPE_LIST.filter(item=>item.value==rentFlag)[0].label
    this.setData({
      params: JSON.parse(options.params),
      propertyName,
      tradeTypesName,
      rentFlagName,
      address
    })
    this.getCityList();
  },

  //获取城市列表
  getCityList(){
    wx.showLoading({
      title: '加载中..',
    })
    app.request({
      url: '/common/getCityList?provinceId=11&geoLevel=2',
      success: r=>{
        let cityName = r.data.filter(item=>item.id==this.data.params.cityId)[0].name
        this.setData({ cityList: r.data, cityName})
        this.getAreaList();
      }
    })
  },

  //选择区域街道
  getAreaList(){
    let url = `https://uniland-tst.oss-cn-hangzhou.aliyuncs.com/common/streetshapes/${this.data.params.cityId}.json`
    wx.request({
      url,
      method: 'GET',
      success: r=>{
        wx.hideLoading()
        if(this.data.params.areaId){
          let Area = r.data.filter(item=>item.id==this.data.params.areaId)[0];
          let streetList = Area.streets;
          if(this.data.params.streetId){
            let streetName = streetList.filter(item=>item.id==this.data.params.streetId)[0].name;
            this.setData({ streetName })
          }
          this.setData({ areaName: Area.name, streetList })
        }
        this.setData({ areaList: r.data  })
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})